/**
 * @Description: 百度在线地图
 * @Author: xubaoliang
 * @Date: 2022-01-25 09:19:27
 */
import TileGrid from "ol/tilegrid/TileGrid";
import TileImage from "ol/source/TileImage";
import TileLayer from "ol/layer/Tile";
import BMapStyle from "../theme/bmap/index";

let BMapLine = {
  initMapLayer: function (hiMapConfig) {
    let resolutions = [];
    for (let i = 0; i <= 19; i++) {
      resolutions[i] = Math.pow(2, 18 - i);
    }
    let tileGrid = new TileGrid({
      // origin: [-1657,26456],
      // origin: [-144,25396],
      origin: [0, 0],
      resolutions: resolutions
    });
    let mapSource = new TileImage({
      projection: 'BD:09', //'EPSG:3857',
      tileGrid: tileGrid,
      crossOrigin: 'anonymous',
      tileUrlFunction: function (tileCoord) {
        if (!tileCoord) {
          return "";
        }
        let z = tileCoord[0];
        let x = tileCoord[1];
        let y = -tileCoord[2] - 1;
        // let y = tileCoord[2];
        let urlNum = (Math.abs(x + y) % 4).toString();
        if (x < 0) {
          x = "M" + (-x).toString();
        }
        if (y < 0) {
          y = "M" + (-y).toString();
        }
        if (!hiMapConfig.mapStyle || hiMapConfig.mapStyle === '') {
          return "https://maponline" + urlNum + ".bdimg.com/tile/?qt=vtile&x=" + x + "&y=" + y + "&z=" + z
            + "&ak=" + hiMapConfig.mapKey + "&styles=pl&scaler=1&udt=" + Date.parse('yyyyMMdd') + "&from=jsapi2_0";
        } else {
          return "https://api.map.baidu.com/customimage/tile?&x=" + x + "&y=" + y + "&z=" + z
            + "&styles=" + BMapStyle[hiMapConfig.mapStyle] + "&scaler=1&udt=20211014&from=jsapi2_0";
        }
      }
    });
    let mapLayer = new TileLayer({
      id: 'baseLayer',
      source: mapSource
    });

    return mapLayer;
  }
}

export default BMapLine;
